#!/usr/bin/env python
#
#

try:
  from xml.etree import ElementTree
except ImportError:
  from elementtree import ElementTree
import gdata.calendar.service
import gdata.service
import atom.service
import gdata.calendar
import atom
import getopt
import sys
import string
import time

class CalendarQuerier:

  def __init__(self):
    """Creates a CalendarService and provides ClientLogin auth details to it.
    The email and password are required arguments for ClientLogin.  The 
    CalendarService automatically sets the service to be 'cl', as is 
    appropriate for calendar.  The 'source' defined below is an arbitrary 
    string, but should be used to reference your name or the name of your
    organization, the app name and version, with '-' between each of the three
    values.  The account_type is specified to authenticate either 
    Google Accounts or Google Apps accounts.  See gdata.service or 
    http://code.google.com/apis/accounts/AuthForInstalledApps.html for more
    info on ClientLogin.  NOTE: ClientLogin should only be used for installed 
    applications and not for multi-user web applications."""
    
    self.service = gdata.calendar.service
    self.cal_client = self.service.CalendarService()
    self.cal_client.source = 'AgileHorizon-InvoiceUpdater-0.1'
        
  def printAllEventsOnCalendar(self):
    """Retrieves all events on the primary calendar for the authenticated user.
    In reality, the server limits the result set intially returned.  You can 
    use the max_results query parameter to allow the server to send additional
    results back (see query parameter use in DateRangeQuery for more info).
    Additionally, you can page through the results returned by using the 
    feed.GetNextLink().href value to get the location of the next set of
    results."""
  
    #feed = self.cal_client.GetCalendarEventFeed(uri="/calendar/feeds/default/private/full")
    
    # Link comes from calendar.link[0]
    #feed = self.cal_client.GetCalendarEventFeed(uri="/calendar/feeds/c5enhn71tcthratrhb5v4pu8f0%40group.calendar.google.com/private/full")
    #calendarEventQuery = self.service.CalendarEventQuery('default')
    
    # Temporarily using the private feed url
    calendarEventQuery = self.service.CalendarEventQuery("c5enhn71tcthratrhb5v4pu8f0@group.calendar.google.com", "private-7e184f703c09a6879b5f2015e75c9245", "full")

    calendarEventQuery.start_min = '2009-12-01'
    calendarEventQuery.start_max = '2009-12-31'
    
    feed = self.cal_client.CalendarQuery(calendarEventQuery)
    
    print 'Events on Calendar: %s' % (feed.title.text,)
    for i, an_event in zip(xrange(len(feed.entry)), feed.entry):
      print '\t%s. event name: %s' % (i, an_event.title.text)
      
      for a_when in an_event.when:
        print '\t\tStart time: %s' % (a_when.start_time,)
        print '\t\tEnd time:   %s' % (a_when.end_time,)

      # For all these events, chuck the duration into a container for that day.
      # Iterate through all the days, summing for each week (?)
      
      #each day - dict where the key is the day of the month, then run though this dict for each week

class Invoicer(object):
    def __init__(self, calendarService, spreadsheetService):
        self.calendarService = calendarService
        self.spreadsheetService = spreadsheetService
    
    def updateInvoiceForMonth(self, year, month):
        self.calendarService.getDailyTotalsForMonth(year, month)

class SpreadsheetService(object):
    pass

class CalendarService(object):
    def getDailyTotalsForMonth(self, year, month):
        pass

if __name__ == "__main__":
    querier = CalendarQuerier()
    querier.printAllEventsOnCalendar()
    
